﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Forum
{
    public partial class NewThread : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btncreate_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                ForumThread forumthread = new ForumThread();
                ForumMessage forummsg = new ForumMessage();
                var user = db_connect.Members.Where(m => m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();
                forumthread.forumidFK = Convert.ToInt32(Request.QueryString["t"].Trim());
                forumthread.threadtitle = txtthreadtitle.Text.Trim();
                forumthread.views = 0;
                forumthread.datecreated = DateTime.Now;
                forumthread.disabled = false;
                forumthread.memberidFK = user.memberidPK;
                db_connect.ForumThreads.Add(forumthread);
                db_connect.SaveChanges();

                forummsg.dateposted = DateTime.Now;
                forummsg.disabled = false;
                forummsg.forumthreadidFK = forumthread.forumthreadidPK;
                forummsg.message = txtforummessage.Text.Trim();
                forummsg.memberidFK = user.memberidPK;
                db_connect.ForumMessages.Add(forummsg);
                db_connect.SaveChanges();

                Response.Redirect("~/Forum/ShowMessage.aspx?t=" + forumthread.forumthreadidPK);
            }
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Forum/Thread.aspx?t=" + Request.QueryString["t"].Trim());
        }
    }
}